﻿using System;
using UnityEngine;

namespace Assets.AnimationPlayer.Scripts.Util
{
    public class ArScale : MonoBehaviour
    {
        // 记录上一次手机触摸位置判断用户是在做放大还是缩小手势
        private Vector2 _oldPosition1 = new Vector2(float.MaxValue, float.MaxValue);
        private Vector2 _oldPosition2 = new Vector2(float.MaxValue, float.MaxValue);


        //private Vector2 _touchPosition = new Vector2(float.MaxValue, float.MaxValue);

        private Vector3 _baseScale = Vector3.one;
        public float ScaleNum = 0.1f;
        public float ScaleMax = 2.0f;
        public float ScaleMin = 0.5f;

        private float _currentScale = 1.0f;
        internal void Start()
        {
            _baseScale = transform.localScale;
        }


        internal void Update()
        {
            //new code 
            if (Input.touchSupported)
            {
                //Input.multiTouchEnabled = false; //禁止多点触摸

                #region 触屏操作

                if (Input.touchCount == 1)
                {
                    //单指旋转
                    if (Input.GetTouch(0).phase == TouchPhase.Began)
                    {
                        //_touchPosition = Input.GetTouch(0).position;
                    }
                    else if (Input.GetTouch(0).phase == TouchPhase.Moved)
                    {
                        var tempPosition = Input.GetTouch(0).position;
                        //Vector2 deltaPosition = new Vector2(tempPosition.x - _touchPosition.x, tempPosition.y - _touchPosition.y);
                        //_touchPosition = tempPosition;

                        //上半部分的滑动，旋转方向取反
                        if (Input.GetTouch(0).position.y >= Screen.height / 2.0f)
                        {
                            //deltaPosition.x = -deltaPosition.x;
                        }
                    }
                }
                else if (Input.touchCount > 1)

                {
                    //两指缩放
                    // 前两只手指触摸类型都为移动触摸
                    if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
                    {

                        // 计算出当前两点触摸点的位置
                        var tempPosition1 = Input.GetTouch(0).position;
                        var tempPosition2 = Input.GetTouch(1).position;

                        if (Math.Abs(_oldPosition1.x - float.MaxValue) <=0&& Math.Abs(_oldPosition2.x - float.MaxValue) <=0)
                        {
                            // 备份上一次触摸点的位置，用于对比
                            _oldPosition1 = tempPosition1;
                            _oldPosition2 = tempPosition2;
                            return;
                        }

                        // 函数返回1为放大，返回-1为缩小
                        int temp = isEnlarge(_oldPosition1, _oldPosition2, tempPosition1, tempPosition2);
                        if (temp > 0)
                        {
                            set_max();
                        }
                        else if (temp < 0)
                        {
                            set_min();
                        }

                        // 备份上一次触摸点的位置，用于对比
                        _oldPosition1 = tempPosition1;
                        _oldPosition2 = tempPosition2;
                    }
                    if (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Ended ||
                        Input.GetTouch(0).phase == TouchPhase.Canceled ||
                        Input.GetTouch(1).phase == TouchPhase.Canceled)
                    {
                        //清空存储的点
                        _oldPosition1 = new Vector2(float.MaxValue, float.MaxValue);
                        _oldPosition2 = new Vector2(float.MaxValue, float.MaxValue);
                    }
                }

                #endregion
            }
            else
            {
                #region 鼠标操作
                if (Input.GetMouseButton(0))
                {

                    //上半部分的滑动，旋转方向取反
                    if (Input.mousePosition.y >= Screen.height / 2.0f)
                    {


                    }
                    else
                    {

                    }


                }


                float cz = Input.GetAxis("Mouse ScrollWheel");
                if (cz < 0)
                {
                    if (Camera.main.fieldOfView <= 100)
                        Camera.main.fieldOfView = Mathf.Min(Camera.main.fieldOfView + 2, 100);
                    else
                        Camera.main.fieldOfView = 100;
                    if (Camera.main.orthographicSize <= 20)
                        Camera.main.orthographicSize += 0.5F;
                }
                else if(cz > 0)
                {
                    if (Camera.main.fieldOfView > 2)
                        Camera.main.fieldOfView -= 2;
                    if (Camera.main.orthographicSize >= 1)
                        Camera.main.orthographicSize -= 0.5F;
                }
                #endregion
            }
        }


        public void set_max()
        {
            if (_currentScale < ScaleMax)
            {
                _currentScale = Mathf.Min(_currentScale + ScaleNum, ScaleMax);
            }
            else
            {
                _currentScale = ScaleMax;
            }
            transform.localScale = _baseScale * _currentScale;
        }

        public void set_min()
        {
            if (_currentScale > ScaleMin)
            {
                _currentScale = Mathf.Max(_currentScale - ScaleNum, ScaleMin);
            }
            else
            {
                _currentScale = ScaleMin;
            }
            transform.localScale = _baseScale * _currentScale;
        }

        // 函数返回真为放大，返回假为缩小
        private int isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
        {
            // 函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
            float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
            float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

            leng1 = Vector2.Distance(oP1, oP2);
            leng2 = Vector2.Distance(nP1, nP2);

            if (leng1 + 5 < leng2)
            {
                // 放大手势
                return 1;
            }
            else if (leng1 - 5 > leng2)
            {
                // 缩小手势
                return -1;
            }
            return 0;
        }
    }
}